<%@ include file="/WEB-INF/jsp/taglib.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>区块详情</title>

    <!-- Bootstrap -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/Transaction.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


    <script type="text/javascript" src="/js/web3.min.js"></script>
    <script type="text/javascript" src="/js/bignumber.js"></script>

    <script src="/js/jquery-2.2.3.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/Transaction.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            // 初始化web3
            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            } else {
                web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
            }

            var blockId = '${blockId}';
            var block = web3.eth.getBlock(blockId);

            var unixTimestamp = new Date(block.timestamp * 1000 );
            var blockTime = unixTimestamp.toLocaleString();

            var url = "/certification/show/" + block.extraData;

            console.log(url);

            $.ajax({
                url: url,
                method: "post",
                success: function (data) {

                    if(data.msg == "success") {

                        var showCertification = '<a target="_blank" href=' + data.filePath + '>查看证书</a>';

                        var downloadCertification = '<a href=' + '/certification/download/' + data.filePath + '>下载证书</a>';

                        var blockDetail = document.getElementById("block-detail");
                        var str = "<tr><td>Number</td>" +
                            "<td>" + block.number + "</td></tr>" +
                            "<tr><td>Hash</td>" +
                            "<td>" + block.hash + "</td></tr>" +
                            "<tr><td>Miner</td>" +
                            "<td>" + block.miner + "</td></tr>" +
                            "<tr><td>Nonce</td>" +
                            "<td>" + block.nonce + "</td></tr>" +
                            "<tr><td>Size</td>" +
                            "<td>" + block.size  + "</td></tr>" +
                            "<tr><td>Time</td>" +
                            "<td>" + blockTime + "</td></tr>" +
                            "<tr><td>Extra</td>" +
                            "<td>" + block.extraData + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + showCertification + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + downloadCertification +   "</td></tr>";
                        blockDetail.innerHTML = str;
                    }else{

                        var blockDetail = document.getElementById("block-detail");
                        var str = "<tr><td>Number</td>" +
                            "<td>" + block.number + "</td></tr>" +
                            "<tr><td>Hash</td>" +
                            "<td>" + block.hash + "</td></tr>" +
                            "<tr><td>Miner</td>" +
                            "<td>" + block.miner + "</td></tr>" +
                            "<tr><td>Nonce</td>" +
                            "<td>" + block.nonce + "</td></tr>" +
                            "<tr><td>Size</td>" +
                            "<td>" + block.size  + "</td></tr>" +
                            "<tr><td>Time</td>" +
                            "<td>" + blockTime + "</td></tr>" +
                            "<tr><td>Extra</td>" +
                            "<td>" + block.extraData + "</td></tr>";
                        blockDetail.innerHTML = str;
                    }
                }
            });
        });

    </script>

    <c:import url="/WEB-INF/jsp/frontend/head.jsp"/>
    <c:import url="/WEB-INF/jsp/frontend/activity/script.jsp"/>
</head>
<body>

<div class="head">
    <c:import url="/WEB-INF/jsp/frontend/header.jsp"/>
</div>

<div class="container-wrap transaction-body">
    <div class="container">
        <div class="row transaction-container">
            <div class="col-md-11 transaction-head">区块详情</div>
            <div class="col-md-11 transaction-divider"></div>
            <div class="col-md-11 table-responsive transaction-table">
                <table class="table table-bordered" id="block-detail">

                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>
